package com.example.shiyun.Adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.shiyun.Pojo.Pojo_citydata;
import com.example.shiyun.Pojo.Pojo_index_listview;
import com.example.shiyun.R;
import com.example.shiyun.Util.Util;
import org.litepal.LitePal;
import java.util.List;

import static android.view.Gravity.CENTER;

/**
 * 首页的Recyclerview的适配器
 */
public class Index_RecyclerAdaper extends RecyclerView.Adapter<Index_RecyclerAdaper.MyHolder> {
    //组件
    private ImageView back,detailed_t_weather;
    private TextView detailed_date,detailed_sunrise,detailed_weather,
            detailed_sunset,detailed_manner,detailed_temperature,detailed_tips;
    //全局
    private List<Pojo_index_listview> list;
    private Context context;
    private View view;
    private  PopupWindow popWindow;
    private boolean flag=true;
    public Index_RecyclerAdaper(List<Pojo_index_listview> list, Context context) {
        this.list = list;
        this.context = context;
    }

    @NonNull
    @Override
    public MyHolder onCreateViewHolder(@NonNull final ViewGroup parent, int viewType) {
        view = LayoutInflater.from(parent.getContext()).inflate(R.layout.mode_index_listview, parent, false);
        final MyHolder holder = new MyHolder(view);

        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull MyHolder holder, int position) {
        Pojo_index_listview mode = list.get(position);
        holder.week.setText(mode.getMode_week());
        holder.date.setText(mode.getMode_date());
        holder.weather.setText(mode.getMode_weather());
        holder.temperature.setText(mode.getMode_temperature());
        holder.weather_chart.setImageResource(mode.getMode_weather_chart());
        //点击
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Recycler_click(view, position);
            }
        });

    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    class MyHolder extends RecyclerView.ViewHolder {
        TextView week, date, weather, temperature;    //周、日期、天气、温度
        ImageView weather_chart;  //天气图

        public MyHolder(@NonNull View v) {
            super(v);
            week = v.findViewById(R.id.mode_week);
            date = v.findViewById(R.id.mode_date);
            weather = v.findViewById(R.id.mode_weather);
            temperature = v.findViewById(R.id.mode_temperature);
            weather_chart = v.findViewById(R.id.mode_weather_chart);
        }
    }

    //点击事件
    private void Recycler_click(View view, int position) {
        //获取视图
        View detailed = LayoutInflater.from(view.getContext()).inflate(R.layout.detailed, null);
        //找视图组件
        detailed_date=detailed.findViewById(R.id.detailed_date);
        detailed_t_weather=detailed.findViewById(R.id.detailed_t_weather);
        detailed_weather=detailed.findViewById(R.id.detailed_weather);
        detailed_sunrise=detailed.findViewById(R.id.detailed_sunrise);
        detailed_sunset=detailed.findViewById(R.id.detailed_sunset);
        detailed_manner=detailed.findViewById(R.id.detailed_manner);
        detailed_temperature=detailed.findViewById(R.id.detailed_temperature);
        detailed_tips=detailed.findViewById(R.id.detailed_tips);
        //设置数据
        List<Pojo_citydata> detaileddata= LitePal.findAll(Pojo_citydata.class);
        detailed_date.setText(detaileddata.get(position).getYmd());
        detailed_t_weather.setImageResource(Util.weather_chart(detaileddata.get(position).getType()));
        detailed_weather.setText(detaileddata.get(position).getType());
        detailed_sunrise.setText("日出："+detaileddata.get(position).getSunrise());
        detailed_sunset.setText("日落："+detaileddata.get(position).getSunset());
        detailed_manner.setText("风度："+detaileddata.get(position).getFx()+detaileddata.get(position).getFl());
        String low = detaileddata.get(position).getLow().substring(3);
        String high = detaileddata.get(position).getHigh().substring(3);
        detailed_temperature.setText("今日温度："+low+"~"+high);
        detailed_tips.setText("温馨提示："+detaileddata.get(position).getNotice());
        if (flag)
        {
            //设置弹出视图
            popWindow = new PopupWindow(detailed, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, true);
            popWindow.setAnimationStyle(R.style.PopupWindowAnimStyle);  //设置加载动画
            popWindow.setTouchable(true);
            popWindow.showAtLocation(detailed, CENTER, 0, 0);
            flag=false;
        }

        back = detailed.findViewById(R.id.back);
        //点击去除
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                popWindow.dismiss();
                flag=true;
            }
        });
        popWindow.setTouchInterceptor(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                return false;
                // 这里如果返回true的话，touch事件将被拦截
                // 拦截后 PopupWindow的onTouchEvent不被调用，这样点击外部区域无法dismiss
            }
        });
    }
}
